﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MrBrooks.Processing
{
    /// <summary>
    /// 防火墙
    /// </summary>
    internal class TcpFireWall
    {
        /// <summary>
        /// 限制地址
        /// </summary>
        private string limitAddress = "0.0.0.0";

        /// <summary>
        /// 防火墙功能是否开启
        /// </summary>
        private bool fireWallActive = false;

        /// <summary>
        /// 计时线程
        /// </summary>
        private Thread thrTimer;

        /// <summary>
        /// 限制的IP地址(包含0.0.0.0则防火墙无效)
        /// </summary>
        public string LimitAddress
        {
            get
            {
                return limitAddress;
            }
            set
            {
                fireWallActive = value.Contains("0.0.0.0");
                limitAddress = value;
                Init();
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="limitAddress"></param>
        public TcpFireWall(string limitAddress)
        {
            this.LimitAddress = limitAddress;
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            if (fireWallActive)
            {
                StartTimer();
            }
            else
            {
                StopTimer();
            }
        }

        /// <summary>
        /// 开始检测线程
        /// </summary>
        private void StartTimer()
        {
            if (thrTimer == null)
            {
                thrTimer = new Thread(new ThreadStart(TimerChecker));
                thrTimer.Start();
            }
        }

        /// <summary>
        /// 停止检测线程
        /// </summary>
        private void StopTimer()
        {
            if (thrTimer != null)
            {
                try
                {
                    thrTimer.Abort();
                    thrTimer = null;
                }
                catch { }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void TimerChecker()
        {
            while (true)
            {
                Thread.Sleep(2000);
            }
        }

        /// <summary>
        /// 监控该地址
        /// </summary>
        /// <param name="address"></param>
        public bool CheckFilter(string address)
        {

            return true;
        }
    }
}
